<!--
 * @Author: ૮₍ ˙О˙₎ა
 * @Date: 2023-07-18 08:25:51
-->
<template>
  <el-form label-width="120px" :inline="true" :disabled="disabled === 2 || disabled === 3">
    <el-form-item label="一级分类">
      <el-select v-model="category1Id" placeholder="请选择" @change="getCategory2">
        <el-option
          v-for="item in category1List"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="一级分类">
      <el-select v-model="category2Id" placeholder="请选择" @change="getCategory3">
        <el-option
          v-for="item in category2List"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="三级分类">
      <el-select v-model="category3Id" placeholder="请选择">
        <el-option
          v-for="item in category3List"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        />
      </el-select>
    </el-form-item>
  </el-form>
</template>

<script lang="ts" setup>
import { useCategoryStore } from '@/stores/category'
import { storeToRefs } from 'pinia'
import { onMounted } from 'vue'

defineProps<{
  disabled?: number
}>()

const categoryStore = useCategoryStore()
const { category1Id, category1List, category2Id, category2List, category3Id, category3List } =
  storeToRefs(categoryStore)

const getCategory3 = () => {
  category3List.value = []
  category3Id.value = ''
  categoryStore.getCategory3List(category2Id.value as number)
}

const getCategory2 = () => {
  category2List.value = category3List.value = []
  category2Id.value = category3Id.value = ''
  categoryStore.getCategory2List(category1Id.value as number)
}

onMounted(() => {
  categoryStore.getCategory1List()
})
</script>

<script lang="ts">
export default {
  name: 'CategoryView'
}
</script>

<style lang="scss" scoped></style>
